<template>
  <div class="search-history">
    <van-cell title="搜索历史">
     <div v-if="isDeleteShow">
       <span @click="$emit('clear-search-histories',[])">全部删除</span>
       &nbsp;&nbsp;
      <span @click="isDeleteShow=false">完成</span>
     </div>
      <van-icon 
      name="delete"
       v-else
       @click="isDeleteShow=true"
       ></van-icon>
    </van-cell>
    <van-cell
      v-for="(item, index) in searchHistories"
      :key="index"
      :title="item"
      @click="onSearchItemClick(item,index)"
    >
      <van-icon name="close" v-show="isDeleteShow"></van-icon>
    </van-cell>
  </div>
</template>
<script>

export default {
  data(){
    return{
      isDeleteShow:false
    }
  },
  props: {
    searchHistories: {
      type: Array,
      required: true,
    },
  },
  methods:{
    onSearchItemClick(item,index){
      if(this.isDeleteShow){
        // 删除状态，删除历史记录数据数组
        // props自定义属性searchHistories是只读的，不可以直接赋值，但是可以修改数据，比如增删，替换部分内容
        // 如果要赋值，则只能通过父组件进行操作
        this.searchHistories.splice(index,1)
      }else{
        // 搜索状态
        this.$emit('search',item)
      }
    }
  }
};
</script>

<style>
</style>